import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcom from '../components/welcom.vue'
import users from '../components/user/users.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
import cate from '../components/goods/cate.vue'
import params from '../components/goods/params.vue'
import GoodsList from '../components/goods/list.vue'
import add from '../components/goods/add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)

const router =  new VueRouter({
  routes : [
    {path:'/',redirect:'/login'},
    {path:'/login',component:login},
    {path:'/home',component:home,
    redirect:'/welcom',
    children:[
      {path:'/welcom',component: welcom},
      {path:'/users',component:users},
      {path:'/rights',component:rights},
      {path:'/roles',component:roles},
      {path:'/categories',component:cate},
      {path:'/params',component:params},
      {path:'/goods',component:GoodsList},
      {path:'/add',component:add},
      {path:'/orders',component:Order},
      {path:'/reports',component:Report}
    ]}
  ]
 })

//  挂载路由导航守卫
router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
 export default router



